<?php

namespace ABonus\Bundle\ExtractBundle\Manager;

use Doctrine\ORM\EntityManager;
use ABonus\Bundle\ExtractBundle\Entity;
use ABonus\Bundle\ExtractBundle\Repository;

/**
 * Description of BlocManager
 *
 */
class BlocManager
{
    protected $repository;
    protected $em;
    protected $kernel;
    protected $imageManager;

    public function __construct(EntityManager $em, $entityClass, $kernel, $imageManager)
    {
        $this->em = $em;
        $this->repository = $em->getRepository($entityClass);
        $this->kernel = $kernel;
        $this->imageManager = $imageManager;
    }

    public function getRepository(){
        return $this->repository;
    }

    public function saveBloc($newBloc){
        $oldBloc = $this->repository->findOneBy(array('name' => $newBloc->getName()));
        if($oldBloc == null){
            $this->em->persist($newBloc);
            $this->em->flush();
        }else{
            $oldBloc->setName($newBloc->getName());
            $oldBloc->setGrade($newBloc->getGrade());
            $oldBloc->setTypes($newBloc->getTypes());
            $oldBloc->setDescription($newBloc->getDescription());
            $oldBloc->setImage($newBloc->getImage());
            $oldBloc->setSector($newBloc->getSector());
            $this->em->flush();
        }
    }


    public function createImageAndAttachItToBloc($bloc, $imageUrl){
        $destination = $this->copyRemoteFile($imageUrl);
        $image = new Entity\Image();
        //TODO enlever extension du nom
        $image->setPath($destination);
        $fileNamewithoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $this->after_last('/', $destination));
        $image->setName($fileNamewithoutExt);
        $image = $this->imageManager->saveImage($image);
        $image->upload();
        $bloc->setImage($image);
        $this->saveBloc($bloc);
    }


    public function copyRemoteFile($imageUrl){
        $saveto =$this->kernel->getRootDir()."/../web/uploads/images";
        $imageName = explode("/", $imageUrl);
        $imageName = end($imageName);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $imageUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec ($ch);
        curl_close ($ch);
        $destination = $saveto."/".$imageName;
        $file = fopen($destination, "w+");
        fputs($file, $data);
        fclose($file);
        return $destination;
    }

    private function after_last($searchFor, $inthat)
    {
        if (!is_bool($this->strrevpos($inthat, $searchFor)))
        return substr($inthat, $this->strrevpos($inthat, $searchFor)+strlen($searchFor));
    }

    // use strrevpos function in case your php version does not include it
    private function strrevpos($instr, $needle)
    {
        $rev_pos = strpos (strrev($instr), strrev($needle));
        if ($rev_pos===false) return false;
        else return strlen($instr) - $rev_pos - strlen($needle);
    }

}
